﻿//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Expenses.ViewModels;
using Expenses.ExpenseService;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using Windows.ApplicationModel.DataTransfer;
using System.Text;
using Windows.Storage.Streams;
using Windows.UI.StartScreen;
using Windows.UI.ViewManagement;

namespace Expenses.Views
{
    public sealed partial class ExpenseReportsHistoryPage : Expenses.Common.LayoutAwarePage
    {
        ExpenseReportsViewModel expenseReportsViewModel = null;
        private DataTransferManager dataTransferManager = null;

        public ExpenseReportsHistoryPage()
        {
            this.InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            GetExpenseReports();

            // Register handler for DataRequested events for sharing
            dataTransferManager = DataTransferManager.GetForCurrentView();
            dataTransferManager.DataRequested += new TypedEventHandler<
                DataTransferManager, DataRequestedEventArgs>(OnDataRequested);
        }

        private async void GetExpenseReports()
        {
            ExpenseReportsHistoryProgressRing.IsActive = true;

            expenseReportsViewModel = new ExpenseReportsViewModel();
            var expenseReports = new ObservableCollection<ExpenseReportViewModel>();
            expenseReports = await
                expenseReportsViewModel.GetApprovedExpenseReports();

            ExpenseReportsViewSource.Source = expenseReports;
            GroupedExpenseReportsViewSource.Source = 
                expenseReportsViewModel.GroupedExpenseReports;
            GroupedExpenseReportsSemanticOut.Source = 
                expenseReportsViewModel.GroupedExpenseReports;

            decimal amountOfExpenseReports = Convert.ToDecimal(
                                             (from report in expenseReports
                                              select report.Amount).Sum());

            if (amountOfExpenseReports > 0)
            {
                PageTitleTextBlock.Text = string.Format(
                    "Past expense reports ({0} for {1:C})",
                    expenseReports.Count(), amountOfExpenseReports);
            }
            else
            {
                PageTitleTextBlock.Text = "You have no past expense reports";
            }

            ExpenseReportsHistoryProgressRing.IsActive = false;
        }

        private void ExpenseReportsGridView_ItemClick(object sender, ItemClickEventArgs e)
        {
            App.CurrentExpenseReport = 
                (ExpenseReportViewModel)e.ClickedItem;
            this.Frame.Navigate(typeof(ExpenseReportPage));
        }

        private void BackButton_Click(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(MainPage));
        }

        void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs args)
        {
            var request = args.Request;
            request.FailWithDisplayText("This page does not support sharing.");
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            dataTransferManager.DataRequested -= new TypedEventHandler<
                DataTransferManager, DataRequestedEventArgs>(OnDataRequested);
            App.LastPage = "ExpenseReportsHistoryPage";
        }

        public static Rect GetElementRect2(FrameworkElement element)
        {
            GeneralTransform buttonTransform = element.TransformToVisual(null);
            Point point = buttonTransform.TransformPoint(new Point());
            return new Rect(point, new Size(element.ActualWidth, element.ActualHeight));
        }

    }
}
